<?php
/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2017/12/20
 * Time: 下午2:34
 */
class Box_model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
        $this->load->helper("common");
    }

    // 保存箱子污损数据
    public function save_box_stained($requst)
    {
        // 数据
        $data_arr['box_id']         = $requst['box_id'];
        $data_arr['submitter_id']   = $requst['submitter_id'];
        $data_arr['submitter_type'] = $requst['submitter_type'];
        $data_arr['stain_types']    = $requst['stain_types'];
        $data_arr['content']        = isset($requst['content']) ? html_escape($requst['content']) : '';
        $data_arr['created_at']     = date('Y-m-d H:i:s');
        $data_arr['handled']        = 0;
        $data_arr['status']         = 0;
        $this->db->insert("app_box_stained", $data_arr);
        $insert_id = $this->db->insert_id();
        if ($insert_id) {
            return true;
        } else {
            return false;
        }
    }

    // 查询箱子详情
    public function query_box_info($id)
    {
        $sql               = "SELECT * FROM app_box WHERE id = $id;";
        $box_info          = $this->db->query($sql)->row_array();
        $box_info['time']  = ceil((time() - strtotime($box_info['created_at'])) / 60 / 60 / 24);
        $box_info['money'] = $this->db->where(array('key' => 'rent'))->from('app_key_value')->get()->row_array()['value'];
        return $box_info;
    }

    // 箱子开关状态
    public function query_box_lid_status($box_id)
    {
        $box_bn = $this->db->query("select box_bn from app_box WHERE id = $box_id")->row_array()['box_bn'];
        $url    = $this->config->item("suda_base_url") . "FrameMessage.aspx?get:$box_bn";
        //发送数据
        $get_data = curl_request($url);
        if ($get_data) {
            $return_info = json_decode($get_data, true);
            if ($return_info['state']['code'] == 0) {
                if ($return_info['lid_state'] == 'close') {
                    return 'close';
                } elseif ($return_info['lid_state'] == 'open') {
                    return 'open';
                } else {
                    return false;
                }
            } else {
                return false;
            }

        } else {
            return false;
        }
    }

    // 箱子开关状态 bn
    public function query_box_lid_status_by_orderbn($box_bn)
    {
        $url = $this->config->item("suda_base_url") . "FrameMessage.aspx?get:$box_bn";
        //发送数据
        $get_data = curl_request($url);
        if ($get_data) {
            $return_info = json_decode($get_data, true);
            if ($return_info['state']['code'] == 0) {
                if ($return_info['lid_state'] == 'close') {
                    return 'close';
                } elseif ($return_info['lid_state'] == 'open') {
                    return 'open';
                } else {
                    return false;
                }
            } else {
                return false;
            }

        } else {
            return false;
        }
    }

    // 箱子是否锁住 id
    public function query_box_lock_status($box_id)
    {
        $box_bn = $this->db->query("select box_bn from app_box WHERE id = $box_id")->row_array()['box_bn'];
        $url    = $this->config->item("suda_base_url") . "FrameMessage.aspx?get:$box_bn";
        //发送数据
        $get_data = curl_request($url);
        if ($get_data) {
            $return_info = json_decode($get_data, true);
            if ($return_info['state']['code'] == 0) {
                if ($return_info['lock1_state'] == 'close') {
                    return 'close';
                } elseif ($return_info['lock1_state'] == 'open') {
                    return 'open';
                } else {
                    return false;
                }
            } else {
                return false;
            }

        } else {
            return false;
        }
    }

    // 箱子是否锁住 bn
    public function query_box_lock_status_by_orerbn($box_bn)
    {
        $url = $this->config->item("suda_base_url") . "FrameMessage.aspx?get:$box_bn";
        //发送数据
        $get_data = curl_request($url);
        if ($get_data) {
            $return_info = json_decode($get_data, true);
            if ($return_info['state']['code'] == 0) {
                if ($return_info['lock1_state'] == 'close') {
                    return 'close';
                } elseif ($return_info['lock1_state'] == 'open') {
                    return 'open';
                } else {
                    return false;
                }
            } else {
                return false;
            }

        } else {
            return false;
        }
    }

    // 箱子详情第三方信息
    public function query_box_detail($box_bn)
    {
        $url = $this->config->item("suda_base_url") . "FrameMessage.aspx?get:$box_bn";
        //发送数据
        $get_data = curl_request($url);
        if ($get_data) {
            $return_info = json_decode($get_data, true);
            $res         = '';
            if ($return_info['state']['code'] == 0) {
                $res['sendTime']     = $return_info['sendTime'];
                $res['signalPower']  = $return_info['signalPower'];
                $res['sendTime']     = $return_info['sendTime'];
                $res['signalPower']  = $return_info['signalPower'];
                $res['bright']       = $return_info['bright'];
                $res['electric']     = $return_info['electric'];
                $res['box_type']     = $return_info['box_type'];
                $res['sendTime']     = $return_info['sendTime'];
                $res['lock1_state']  = $return_info['lock1_state'];
                $res['temperature1'] = $return_info['temperature1'];
                $res['wetness1']     = $return_info['wetness1'];
                $res['latitude']     = $return_info['latitude'];
                $res['longitude']    = $return_info['longitude'];
                $res['password']     = $return_info['password'];
                $res['lid_state']    = $return_info['lid_state'];
                return $res;
            } else {
                return '';
            }
        } else {
            return '';
        }

    }

    // 获取所有箱子信息 第三方
    public function query_all_box_detail()
    {
        $url = $this->config->item("suda_base_url") . "FrameMessage.aspx?get:all";
        //发送数据
        $get_data    = curl_request($url);
        $res         = json_decode($get_data, true);
        $return_info = '';
        if ($res) {
            if ($res['state']['code'] == 0) {
                $box_list = $res['Boxes'];
                foreach ($box_list as $box) {
                    $b['box_bn']       = $box['box_id'];
                    $b['serverIP']     = $box['serverIP'];
                    $b['serverPort']   = $box['serverPort'];
                    $b['signalPower']  = $box['signalPower'];
                    $b['bright']       = $box['bright'];
                    $b['electric']     = $box['electric'];
                    $b['box_type']     = $box['box_type'];
                    $b['sendTime']     = $box['sendTime'];
                    $b['lock1_state']  = $box['lock1_state'];
                    $b['temperature1'] = $box['temperature1'];
                    $b['wetness1']     = $box['wetness1'];
                    $b['latitude']     = $box['latitude'];
                    $b['longitude']    = $box['longitude'];
                    $b['password']     = $box['password'];
                    $b['lid_state']    = $box['lid_state'];
                    $local_box_info    = $this->query_box_by_bn($box['box_id']);
                    $b['use_status']   = $local_box_info['use_status'];
                    $b['box_id']       = $local_box_info['id'];
                    $return_info[]     = $b;
                }
                return $return_info;
            }
        } else {
            return '';
        }
    }

    // 所有桩点信息
    public function all_stake_list()
    {
        $sql        = "SELECT * FROM app_stake WHERE 1 = 1 ORDER BY id DESC ;";
        $stake_list = $this->db->query($sql)->result_array();
        if ($stake_list && is_array($stake_list)) {
            foreach ($stake_list as &$sl) {
                $pic_path = $sl['pic'];
                if ($pic_path) {
                    $sl['pic'] = $this->config->item("upload_url") . $pic_path;
                }
            }
        }
        return $stake_list;
    }

    // 根据箱子编号查找箱子
    public function query_box_by_bn($box_bn)
    {
        $sql = "SELECT * FROM app_box WHERE box_bn = '$box_bn';";
        return $this->db->query($sql)->row_array();
    }

    // 所有箱子类型
    public function query_box_type()
    {
        $sql = "SELECT * FROM app_box_type ";
        // 排序
        $sql .= " order by id desc";
        // 分页
        $desc['page']  = isset($requst['page']) ? (int) $requst['page'] : 1;
        $desc['psize'] = isset($requst['psize']) ? (int) $requst['psize'] : 10;
        $limit_from    = ($desc['page'] - 1) * $desc['psize'];
        $sql .= " limit " . $limit_from . "," . $desc['psize'];
        $box_type_list = $this->db->query($sql)->result_array();

        return $box_type_list;
    }

    // 根据桩点ID查询桩点信息
    public function query_stake_info($id)
    {
        $sql        = "SELECT * FROM app_stake WHERE id = $id;";
        $res        = $this->db->query($sql)->row_array();
        $res['pic'] = $this->config->item("upload_url") . $res['pic'];
        return $res;
    }

    // 所有桩点列表
    public function query_stake_list()
    {
        $sql = "SELECT * FROM app_stake;";
        return $this->db->query($sql)->result_array();
    }

    // 桩点箱子信息统计
    public function state_box_info($stake_id)
    {
        $sql = "SELECT
  count(1) AS box_sum,
  ab.type_id,
  abt.name,
  abt.content,
  abt.height,
  abt.length,
  abt.width
FROM app_box AS ab LEFT JOIN app_box_type AS abt ON abt.id = ab.type_id
WHERE ab.stake_id = $stake_id
GROUP BY ab.type_id";
        $state_box_info = $this->db->query($sql)->result_array();

        return $state_box_info;

    }

    // 查询在快递员身边的箱子
    public function query_courier_box($courier_id, $box_id)
    {
        $sql = "select * from app_courier_box WHERE status = 1 AND courier_id = $courier_id AND box_id = $box_id";
        $res = $this->db->query($sql)->row_array();

        return $res;
    }

    // 开箱
    public function open_box($box_bn)
    {
        $url = $this->config->item("suda_base_url") . "FrameMessage.aspx?open:$box_bn";
        //发送数据
        $get_data = curl_request($url);
        if ($get_data) {
            $return_info = json_decode($get_data, true);
            if ($return_info['state']['code'] == 0) {
                return true;
            }
        }
        return false;
    }

    // 关箱
    public function close_box($box_bn)
    {
        $url = $this->config->item("suda_base_url") . "FrameMessage.aspx?close:$box_bn";
        // 发送数据
        $get_data = curl_request($url);
        if ($get_data) {
            $return_info = json_decode($get_data, true);
            if ($return_info['state']['code'] == 0) {
                return true;
            }
        }
        return false;
    }

    // 获取密码
    public function get_password($box_bn)
    {
        $url = $this->config->item("suda_base_url") . "FrameMessage.aspx?encrypt:$box_bn";
        //发送数据
        $get_data = curl_request($url);
        if ($get_data) {
            $return_info = json_decode($get_data, true);
            if ($return_info['state']['code'] == 0) {
                return isset($return_info['password']) ? $return_info['password'] : '';
            }
        }
        return false;
    }

    // 消除密码
    public function delete_password($box_bn)
    {
        $url = $this->config->item("suda_base_url") . "FrameMessage.aspx?delete:$box_bn";
        //发送数据
        $get_data = curl_request($url);
        if ($get_data) {
            $return_info = json_decode($get_data, true);
            if ($return_info['state']['code'] == 0) {
                return true;
            }
        }
        return false;
    }
}
